package com.appkubes.fintech.restful.application.user.controller;


import com.appkubes.base.core.entity.vo.UserKeyVO;
import com.appkubes.fintech.common.response.Response;
import com.appkubes.fintech.core.po.FintechUserContact;
import com.appkubes.fintech.core.po.FintechUserContactExample;
import com.appkubes.fintech.restful.application.user.entity.dto.EmergencyContactDTO;
import com.appkubes.fintech.restful.application.user.entity.vo.UserContactInfoVO;
import com.appkubes.fintech.restful.application.user.service.UserContactService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Api(tags = "用户紧急联系人")
@RestController
@RequestMapping("/user/contact")
public class UserContactController {
    @Resource
    UserContactService contactService;

    @ApiOperation("添加用户紧急联系人信息")
    @PostMapping("addContact")
    public Response saveUserConcatInfo(@RequestBody @Valid UserContactInfoVO userContactInfoVO
            , BindingResult bindingResult) {
//        if (bindingResult.hasErrors() || userContactInfoVO.getEmergencyContactDTOList().size() != 2) {
//            return Response.fail("参数检查失败");
//        }
        Integer effectRow = contactService.saveEmergencyContact(userContactInfoVO);
        return Response.success(effectRow);
    }

    @ApiOperation("查询用户紧急联系人")
    @GetMapping("getContact")
    public Response<List<EmergencyContactDTO>> getEmergencyContactInfo(@Valid UserKeyVO userKeyVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.fail("参数检查失败");
        }

        FintechUserContactExample example = new FintechUserContactExample();
        example.createCriteria().andUserKeyEqualTo(userKeyVO.getUserKey());
        example.setOrderByClause(" relation, realtion_type ");
        List<FintechUserContact> userContactList = contactService.getUserContactDao().selectByExample(example);

        //结果映射
        List<EmergencyContactDTO> result = userContactList.stream().map(userContact -> {
            EmergencyContactDTO contactDTO = new EmergencyContactDTO();
            BeanUtils.copyProperties(userContact, contactDTO);
            return contactDTO;
        }).collect(Collectors.toList());

        return Response.success(result);
    }
}
